package com.liuanbang.saro.sarodemo.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.liuanbang.saro.sarodemo.R;

/**
 * Created by 刘安邦 on 2017/3/20.
 * Email:liuanbang@aliyun.com
 * 描述:VIPView  2017/3/20
 * 修改:
 */
public class VIPView extends View {
    // 坐标点的颜色
    private int mPointColor;
    //曲线颜色
    private int mLineColor;
    //坐标点颜色 已达到状态
    private int mReachPointColor;
    // 坐标点的半径
    private int mPointRadius;
    //字体颜色
    private int mTextColor;
    //当前等级积分
    private double nowGradeAngle = 0;
    private Paint mTextPaint;
    private Paint mReachTextPaint;
    private Paint mLinePaint;
    private Paint mReachLinePaint;
    private Paint mPointPaint;
    private Paint mReachPointPaint;
    private Paint mLocationPaint;
    //曲线半径
    private float radius = 0;
    //每段间隔长度
    private double length = 0;
    //VIP等级数量V1V2...
    private int vipGrade = 6;

    //xy轴距离周边的距离，用于画刻度文本
    private int margin = 50;
    //角度
    private double angle = 0;
    //每段角度
    private double[] angles = new double[vipGrade];
    //VIP点坐标
    private float[] x = new float[vipGrade];
    private float[] y = new float[vipGrade];

    private VIPViewAnmiation anmiation;

    private Context context = null;

    public VIPView(Context context) {
        this(context, null);
    }

    public VIPView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VIPView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VIPView, defStyleAttr, 0);
        mReachPointColor = ta.getColor(R.styleable.VIPView_vipReachPointColor, 0xff381a59);
        mLineColor = ta.getColor(R.styleable.VIPView_vipLineColor, 0xff8e29fa);
        mPointColor = ta.getColor(R.styleable.VIPView_vipPointColor, 0xffff5100);
        mPointRadius = dp2px(3);
        mTextColor = mPointColor;
        ta.recycle();
        //角度是设置
        radius = dp2px(450);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(40);

        mReachTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mReachTextPaint.setStyle(Paint.Style.FILL);
        mReachTextPaint.setColor(mReachPointColor);
        mReachTextPaint.setTextSize(40);

        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(mLineColor);
        mLinePaint.setStrokeWidth(dp2px(1.5f));

        mReachLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mReachLinePaint.setStyle(Paint.Style.STROKE);
        mReachLinePaint.setColor(mReachPointColor);
        mReachLinePaint.setStrokeWidth(dp2px(1.5f));

        mPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setColor(mPointColor);

        mReachPointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mReachPointPaint.setStyle(Paint.Style.FILL);
        mReachPointPaint.setColor(mReachPointColor);
        //发光效果 需要关闭硬件强制渲染
        mReachPointPaint.setMaskFilter(new BlurMaskFilter(2, BlurMaskFilter.Blur.NORMAL));

        mLocationPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLocationPaint.setStyle(Paint.Style.STROKE);
        mLocationPaint.setColor(mReachPointColor);
        mLocationPaint.setStrokeWidth(dp2px(5));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measuredDimension(widthMeasureSpec), measuredDimension(heightMeasureSpec));
    }

    private int measuredDimension(int measureSpec) {
        int result;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = 500;
            if (mode == MeasureSpec.AT_MOST) {
                //  result = Math.min(result, size);
                result = Math.max(result, size);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //关闭 硬件的强制渲染
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        //----------这里暂时设置为了固定值  h2是坐标轴以下圆的Y轴长度 h1是坐标轴以上到圆点Y轴长度-------------
        float h2 = dp2px(123);
        float h1 = (radius - h2);
        //x0 y0 是圆心坐标
        float x0 = 0 + margin;
        float y0 = -h1;
        //length = Math.sqrt(Math.pow(radius, 2) - Math.pow(h1, 2)) / (vipGrade - 1);
        length = getWidth() / (vipGrade + 0.5);
        //绘制圆弧 (四个参数  其实就是 框住这个圆弧的方框)
        RectF rectF = new RectF(-radius + margin, -h1 - radius, radius + margin, h2);
        canvas.drawArc(rectF, 90, -60, false, mLinePaint);
        //绘制刻度点(未到达)
        for (int j = 0; j < vipGrade; j++) {
            //获取Y轴坐标
            float height = (float) (Math.sqrt(Math.pow(radius, 2) - Math.pow(j * length, 2)) - h1);
            //获取X轴坐标
            float width = (float) ((length * j) + margin);
            //获取每段的总角度
            if (j < vipGrade) {
                //将sin值转为角度
                angles[j] = Math.asin((width - margin) / radius) / 2 / Math.PI * 360;
            }
            x[j] = (float) (x0 + radius * Math.cos((90 - angles[j]) * Math.PI / 180));
            y[j] = (float) (y0 + radius * Math.sin((90 - angles[j]) * Math.PI / 180));
            //未达到的进行绘制否则不绘制
            if (angle < (int) angles[j] | angle == 0) {
                //当位于V0时 V0默认为选中状态
                if (j == 0) {
                    continue;
                }
                //绘制坐标点
                canvas.drawCircle(x[j], y[j], dp2px(5), mPointPaint);
                //绘制VIP文字
                canvas.drawText(String.valueOf("V" + j), x[j], y[j] + mTextPaint.getTextSize() + dp2px(5), mTextPaint);
            }
        }
        //画已达到圆弧
        canvas.drawArc(rectF, 90, (float) -angle, false, mReachLinePaint);

        //画位置图标
        Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.img_location);
        //x1 y1 是根据角度计算圆上点的坐标
        float x1 = (float) (x0 + radius * Math.cos((90 - angle) * Math.PI / 180));
        float y1 = (float) (y0 + radius * Math.sin((90 - angle) * Math.PI / 180));

        //画位置图标
        canvas.drawBitmap(bitmap, x1 - dp2px(5), y1 - bitmap.getHeight() - dp2px(10), mLocationPaint);

        //画VIP的坐标点及文字(已达到)
        for (int i = 0; i < vipGrade; i++) {
            if ((int) angle >= (int) angles[i]) {
                canvas.drawCircle(x[i], y[i], dp2px(5), mReachPointPaint);
                canvas.drawText(String.valueOf("V" + i), x[i], y[i] + mTextPaint.getTextSize() + dp2px(5), mReachTextPaint);
            } else {
                return;
            }
        }
    }

    /**
     * 开始动画()
     */
    public void startCustomAnimation(int gradeNumber) {
        anmiation = new VIPViewAnmiation(gradeNumber);
        anmiation.setDuration(500);
        this.startAnimation(anmiation);
    }

    /**
     * 设置坐标点的动画
     * 该动画写法 可用于点击事件时的写法
     */
    public void setPointAnimator() {
        ValueAnimator anim = ValueAnimator.ofInt(0, (int) angles[5]);
        anim.setDuration(500);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                angle = value;
                invalidate();
            }
        });
        anim.start();
    }

    private int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
    }

    /**
     * 该动画写法(用于OnCreatVIew时候可以执行)
     */
    class VIPViewAnmiation extends Animation {
        int gradeNumber = 0;

        public VIPViewAnmiation(int gradeNumber) {
            this.gradeNumber = gradeNumber;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            //interpolatedTime参数从0渐 变为1，当该参数为1时表明动画结束
            if (gradeNumber >= 12000) {
                nowGradeAngle = angles[5];
            } else if (gradeNumber >= 6000) {
                nowGradeAngle = angles[4] + (angles[5] - angles[4]) / 6000 * (gradeNumber - 6000);
            } else if (gradeNumber >= 2500) {
                nowGradeAngle = angles[3] + (angles[4] - angles[3]) / 3500 * (gradeNumber - 2500);
            } else if (gradeNumber >= 1000) {
                nowGradeAngle = angles[2] + (angles[3] - angles[2]) / 1500 * (gradeNumber - 1000);
            } else if (gradeNumber >= 0) {
                nowGradeAngle = angles[1] + (angles[2] - angles[1]) / 1000 * (gradeNumber - 0);
            } else if (gradeNumber < 0) {
                nowGradeAngle = angles[0];
            }
            angle = interpolatedTime * nowGradeAngle;
            postInvalidate();
        }
    }
}
